import WS from '@/utils/socket'
import {
	restartOpen
} from '@/api/user.js'
export default {
	data() {
		return {
			deviceId: '', // 设备id
			orderNo: '',
			socketInfo: {}, // socket推送的消息信息
			$ws: null
		}
	},
	onload() {

	},
	onUnload() {
		if (this.$ws) {
			this.$ws.close()
		}
	},
	beforeDestroy() {
		if (this.$ws) {
			this.$ws.close()
		}
	},
	methods: {
		// 链接设备
		socketInit(res) {
			setTimeout(() => {
				this.socketInfo = res || {}
			}, 1000)

			let uri = `gzyh_device_result_` + res.orderNo
			this.$ws = new WS(uri)
			// 当显示打开几号仓门后，才会执行下面
			this.$ws.onNotify(uri, res => {
				console.log('收到消息了')
				console.log(res, 2222222222222222)
				if (res.code == 200 && res.data.state != -1) {
					console.log('%c 获取到secketInfo数据了', 'font-size:16px;color:red')
					console.log(res.data)
					this.$ws.send({
						msg: JSON.stringify({
							"code": 200,
							"topic": uri,
							"state": res.data.state
						})
					}) //告诉后台推送已收到
					this.isOPen = true
					console.log('重新开门2', this.socketInfo);
					this.socketInfo = res.data
					this.resultHandle(res)
				}
				uni.hideLoading()
			})
		},
	}
}